Multiple frames with Menubar using tkinter

by: mssaidy, 8 years ago

Last edited: 8 years ago

Hello,
I am trying to build a gui with multiple frames using tkinter as shown in the code. At the top I would like to display a menubar in one of the frames. The menubar is not displayed. Please, can you spot the error in the code. Many thanks. mssaidy
from tkinter import *
import tkinter as tk
#import Tkinter as Tk # Tkinter -> tkinter in Python3
root = Tk()

class App(tk.Tk):
    
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
    # create a Frame for the Text and Scrollbar
        container = tk.Frame(self, width=600, height=600)
        container.pack(side="top", fill="both", expand=True)
                
        # implement stretchability
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Start_Page, All_Alarms_Page, Disabled_Alarms_Page):
            frame = F(container, self)
            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Start_Page)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

        
class Start_Page(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        
        # create the file object)
#        menubar = Menu(root)
        menubar = Menu(self.parent)
        
        # create a pulldown menu, and add it to the menu bar
        filemenu = tk.Menu(menubar, tearoff=0)

        filemenu.add_command(label="Load all alarms", command=lambda: controller.show_frame(load_all_alarms))

        filemenu.add_command(label="Clear all alarms", command=lambda: controller.show_frame(clear_all_alarms))

        filemenu.add_command(label="Load disabled alarms", command=lambda: controller.show_frame(load_disabled_alarms))

        filemenu.add_separator()
        
        # adds a command to the menu option, calling it exit, and the
        # command it runs on event is client_exit
        filemenu.add_command(label="Exit", command=lambda: controller.show_frame(client_exit))

        #added "file" to our menu
        menubar.add_cascade(label="File", menu=filemenu)

        # create the file object)
        editmenu = tk.Menu(menubar)

        # adds a command to the menu option, calling it exit, and the
        # command it runs on event is client_exit
        editmenu.add_command(label="Undo")

        #added "file" to our menu
        menubar.add_cascade(label="Edit", menu=editmenu)

        root.configure(menu=menubar)

    # create a Text widget
        txt = Text(self, borderwidth=3, relief="sunken")
        txt.config(font=("consolas", 12), undo=True, wrap='word')
        txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)

    # create a Scrollbar and associate it with txt
        scrollb = Scrollbar(self, command=txt.yview)
        scrollb.grid(row=0, column=1, sticky='nsew')
        txt['yscrollcommand'] = scrollb.set

        print("This is Start Page")        
        

    def load_all_alarms(self):
        print ("Load all Alarms...")

    def clear_all_alarms(self):
        print ("clear all Alarms...")

    def load_disabled_alarms(self):
        print ("Load disabled Alarms...")
  
    def client_exit(self):
        exit()

        

class Disabled_Alarms_Page(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)




class All_Alarms_Page(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        
        #create table headings
#        headings = Frame(root)
#        headings.pack(side=TOP, expand=YES, fill=BOTH)

        L1=Label(self, text='UID32', font='tahoma 8 bold', width=12, background='green')
        L1.pack(side=LEFT, fill=X)

        L2=Label(self, text='FULL PATH', font='tahoma 8 bold',  width=73, background='green')
        L2.pack(side=LEFT, fill=X)

        L3=Label(self, text='ALARM CONFIGURATION', font='tahoma 8 bold', width=115, background='green')
        L3.pack(side=LEFT, fill=X)
    
        L4=Label(self, text='ENABLED', font='tahoma 8 bold', width=10, background='green')    
        L4.pack(side=LEFT, fill=X)

    # create a Text widget
        txt = tk.Text(self, borderwidth=3, relief="sunken")
        txt.config(font=("consolas", 12), undo=True, wrap='word')
        txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)

    # create a Scrollbar and associate it with txt
        scrollb = tk.Scrollbar(self, command=txt.yview)
        scrollb.grid(row=0, column=1, sticky='nsew')
        txt['yscrollcommand'] = scrollb.set

        print("This is All Alarms Page")

if __name__ == "__main__":
    app = App()
    app.title("Alarms Display")
    app.geometry("400x400")
    app.mainloop()




You must be logged in to post. Please login or register an account.